C++ Arrays


Tip
The most popular types of arrays are integer arrays and double arrays. The figure below shows how to declare an array of integers and array of doubles. Each element in an array of integers is an integer value and can be used as any integer value is used. Similarly, each element in an array of doubles is a double value and can be used as any double value is used.
Los tipos de arreglos más populares son los arreglos de enteros y los arreglos de valores de punto flotante. La figura de abajo muestra como declarar un arreglo de enteros y un arreglo de valores de punto flotante. Cada elemento en un arreglo de enteros es un valor entero y puede ser usado como cualquier valor entero es usado. De forma similar, cada elemento en un arreglo de doubles es un valor double y puede ser usado como cualquier double es usado.

ArrayExample

C++ Arrays

C++ was designed to create the fastest and most efficient code, and the indexes of an array are not verified as this could slow down the program. Always use arrays to an appropriate size and do not write outside the array. Even though C++ is criticized because of this, there are some options to verify the indexes of the array and eliminate this problem completely. These criticisms are always produced because the language is not completely understood.
C++ fue diseñado para crear el código más rápido y más eficiente, por lo que no se checan errores en los índices del arreglo que podrían hacer el código más lento. Use siempre arreglos de tamaño apropiado y no escriba más allá del final del arreglo. Aunque C++ es criticado por esto, cabe mencionar que es posible crear código para checar por errores al accesar el arreglo eliminándose este problema por completo. Estas críticas siempre son producidas por desconocimiento del lenguaje.

Problem 1
Compute the output and table of variables of the following code. Create a dialog application called Acuarela to test your results. After creating the project, insert a multiline textbox called tbx1.
Calcule la salida y la tabla de variables del código siguiente. Cree una aplicación de diálogo llamada Acuarela para probar sus resultados. Después de crear el proyecto, inserte una caja de texto multilínea llamada tbx1.

Acuarela.h
#pragma once //______________________________________ Acuarela.h
#include "resource.h"

#define COUNT 5
class Acuarela: public Win::Dialog
{
public:
     Acuarela()
     {
     }
     ~Acuarela()
     {
     }
     . . .
};

Acuarela.cpp
void Acuarela::Window_Open(Win::Event& e)
{
     int i = 0, total = 0;
     int n[COUNT]; // An array of integer values
     wchar_t text[128];

     for (i = 0; i < COUNT; i++) n[i]=2*i;

     for (i = 0; i < COUNT; i++)
     {
          total += n[i];
          _snwprintf_s(text, 128, _TRUNCATE, L"n[%d] = %d -> %d\r\n", i, n[i], total);
          tbx1.Text += text;
     }
}

TableVariableNText

Problem 2
Compute the output and table of variables of the following code. Create a dialog application called Vaso to test your results. After creating the project, insert a multiline textbox called tbx1. This problem illustrates how human text is stored and processed by the computer. Internally, any character is a binary value that can be interpreted as integer value or hexadecimal (or any in other possible way). In this case, 0x7A is a hexadecimal value (122 in decimal) that represents the character z. All characters, sounds, images, etc., that make sense to humans are arrays of bits for the computer.
Calcule la salida y la tabla de variables del código siguiente. Cree una aplicación de diálogo llamada Vaso para probar sus resultados. Después de crear el proyecto, inserte una caja de texto multilínea llamada tbx1. Este problema ilustra como el texto de los humanos es almacenado y procesado por la computadora. Internamente, cualquier letra es un valor binario que puede ser interpretado como un valor entero o hexadecimal (o en cualquier otra forma posible). En este caso, 0x7A es un valor en hexadecimal (122 en decimal) que representa la letra z. Todas las letras, sonidos, imágenes, etc., que tienen significado para los humanos son arreglos de bits para la computadora

CharacterMap

Vaso.h
#pragma once //______________________________________ Vaso.h
#include "resource.h"

#define MAX_VALUE 0x7A
#define MIN_VALUE MAX_VALUE-5
class Vaso: public Win::Dialog
{
public:
     Vaso()
     {
     }
     ~Vaso()
     {
     }
     . . .
};

Vaso.cpp
void Vaso::Window_Open(Win::Event& e)
{
     wchar_t text[64];
     int i = 0;
     for(i = MAX_VALUE; i > MIN_VALUE; i--)
     {
          _snwprintf_s(text, 64, _TRUNCATE, L"%c", i);
          tbx1.Text += text;
          if (i > MIN_VALUE+1) tbx1.Text += L", ";
     }     
}

Problem 3
Compute the output and table of variables of the following code.
Calcule la salida y la tabla de variables del código siguiente.

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

#define COUNT 5
class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     . . .
};

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     int i=0, n[COUNT];
     wchar_t text[64];

     for (i = 0; i <= COUNT; i++) n[i] = i*i;

     for (i = 0; i < COUNT; i++)
     {
          _snwprintf_s(text, 64, _TRUNCATE, L"n[%d] = %d\r\n", i, n[i]);
          tbx1.Text += text;
     }
}

Tip
If an array is of size 5, the only valid index values are: 0, 1, 2, 3 and 4. Never use an index outside of the valid value range as in the previous example.
Si un arrelgo es de tamaño 5, los valores de los índices válidos son: 0, 1, 2, 3 y 4. Nunca use un índice fuera del rango de valores válidos como en el ejemplo anterior.

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home